####################################
# Prepare for first vignette
####################################
rm(list=ls())

# Load packages
library(foreign)
library(lattice)
library(Hmisc)
library(lsmeans)

# Load data
data <- read.dta("Kobayashi_Yokoyama_JJPS_replication.dta")

# Linearly transform each measurement into a disapproval rate ranging from 0 to 100
data$tssl_pdr <- data$polpref_tssl * 100
data$rcs_pdr <- data$polpref_rcs * 100

####################################
# Assign value labels to the variables.
####################################

# Experimental conditions: 1st vignette
data$expcond_1stvign <- factor(data$expcond_1stvign,
                     levels = c(1, 2, 3, 4, 5, 6, 7, 8),
                     labels = c("Double party cue", "LDP cue only", "DPJ cue only", "No party cue",
                                "Double press cue", "Yomiuri cue only", "Asashi cue only", "No press cue"))
# Party identification
data$pid <- factor(data$pid,
                      levels = c(1, 2, 0),
                      labels = c("LDP supporter","DPJ supporter","Independent"))

# Newspaper subscription
data$newssub <- factor(data$newssub,
                      levels = c(3, 1, 2, 0),
                      labels = c("Both subscriber", "Yomiuri subscriber", "Asahi subscriber", "Neither subscriber"))

####################################
# Create subset of data
####################################

first <- c("Double party cue", "LDP cue only", "DPJ cue only", "No party cue")
second <- c("Double press cue", "Yomiuri cue only", "Asashi cue only", "No press cue")

data.first <- data[data$expcond_1stvign %in% first, ]
data.second <- data[data$expcond_1stvign %in% second, ]

####################################
# Figure 1. 
# Treatment effects of party cues on policy preferences with 95% CIs (low-salience issue)
####################################

partycue.lm <- lm(tssl_pdr~expcond_1stvign*pid, data=data.first)

partycue.rg1 <- ref.grid(partycue.lm)
partycue.lsm <- lsmeans(partycue.lm, c("expcond_1stvign", "pid"))
partycue.sum <- summary(partycue.lsm)

clabel <- as.data.frame(paste(as.character(partycue.sum$pid), as.character(partycue.sum$expcond_1stvign)))
partycue.sum2 <- cbind(partycue.sum, clabel)
names(partycue.sum2)[8]<-paste("label")

partycue.sum2$label <- factor(partycue.sum2$label, as.character(partycue.sum2$label))

theme.novpadding <- 
  list(layout.heights = 
         list(top.padding = 3, 
              main.key.padding = 0, 
              key.axis.padding = 0, 
              axis.xlab.padding = 0, 
              xlab.key.padding = 0, 
              key.sub.padding = 0, 
              bottom.padding = 3), 
       layout.widths = 
         list(left.padding = 0, 
              key.ylab.padding = 0, 
              ylab.axis.padding = 0, 
              axis.key.padding = 0, 
              right.padding = 0),
       axis.text=list(cex=1.3),
  par.sub.text=list(cex=1, just="left",x = grid::unit(5, "mm")))

xyplot(expcond_1stvign~lsmean | pid, data=partycue.sum2,
       scales=list(alternating=FALSE,
                   y=list(relation="same")), 
       main=list("Temporary staffing services law", cex=1.3),
       xlab=list("Disapproval rate (%)\n", cex=1.3), ylab=NULL, 
       low95=partycue.sum2$lower.CL, up95=partycue.sum2$upper.CL, 
       panel = function(x,y,subscripts, low95, up95,...){
         panel.segments(low95[subscripts], y, 
                        up95[subscripts], y, col="#000000")
         panel.points(x,y, pch=16, col="#000000")
       },
       xlim=c(min(partycue.sum2$lower.CL)-10, max(partycue.sum2$upper.CL)+15), 
       par.strip.text=list(cex=1.3), 
       par.settings=theme.novpadding,
       layout=c(3,1), as.table=T, between=list(x=0.5))


####################################
# Figure 3. 
# Treatment effects of press cues on policy preferences with 95% CIs (low-salience issue)
####################################

presscue.lm <- lm(tssl_pdr~expcond_1stvign*newssub, data=data.second)

presscue.rg1 <- ref.grid(presscue.lm)
presscue.lsm <- lsmeans(presscue.lm, c("expcond_1stvign", "newssub"))
presscue.sum <- summary(presscue.lsm)
presscue.sum

clabel <- as.data.frame(paste(as.character(presscue.sum$newssub), as.character(presscue.sum$expcond_1stvign)))
presscue.sum2 <- cbind(presscue.sum, clabel)
names(presscue.sum2)[8]<-paste("label")

presscue.sum2$label <- factor(presscue.sum2$label, as.character(presscue.sum2$label))

xyplot(expcond_1stvign~lsmean | newssub, data=presscue.sum2,
       scales=list(alternating=FALSE,
                   y=list(relation="same")), 
       main=list("Temporary staffing services law", cex=1.3),
       xlab=list("Disapproval rate (%)\n", cex=1.3), ylab=NULL, 
       low95=presscue.sum2$lower.CL, up95=presscue.sum2$upper.CL, 
       panel = function(x,y,subscripts, low95, up95,...){
         panel.segments(low95[subscripts], y, 
                        up95[subscripts], y, col="#000000")
         panel.points(x,y, pch=16, col="#000000")
       },
       xlim=c(min(presscue.sum2$lower.CL)-10, max(presscue.sum2$upper.CL)+25), 
       par.strip.text=list(cex=1.3), 
       par.settings=theme.novpadding, 
       layout=c(4,1), as.table=T, between=list(x=0.5))

####################################
# Prepare for second vignette
####################################
rm(list=ls())

# Load packages
library(foreign)
library(lattice)
library(Hmisc)
library(lsmeans)

# Load data
data <- read.dta("Kobayashi_Yokoyama_JJPS_replication.dta")

# Linearly transform each measurement into a disapproval rate ranging from 0 to 100
data$tssl_pdr <- data$polpref_tssl * 100
data$rcs_pdr <- data$polpref_rcs * 100

####################################
# Assign value labels to the variables.
####################################

# Experimental conditions: 2nd vignette
data$expcond_2ndvign <- factor(data$expcond_2ndvign,
                     levels = c(1, 2, 3, 4, 5, 6, 7, 8),
                     labels = c("Double press cue", "Yomiuri cue only", "Asashi cue only", "No press cue",
                                "Double party cue", "LDP cue only", "DPJ cue only", "No party cue"))

# Party identification
data$pid <- factor(data$pid,
                      levels = c(1, 2, 0),
                      labels = c("LDP supporter","DPJ supporter","Independent"))

# Newspaper subscription
data$newssub <- factor(data$newssub,
                      levels = c(3, 1, 2, 0),
                      labels = c("Both subscriber", "Yomiuri subscriber", "Asahi subscriber", "Neither subscriber"))

####################################
# Create subset of data
####################################

first <- c("Double press cue", "Yomiuri cue only", "Asashi cue only", "No press cue")
second <- c("Double party cue", "LDP cue only", "DPJ cue only", "No party cue")

data.first <- data[data$expcond_2ndvign %in% first, ]
data.second <- data[data$expcond_2ndvign %in% second, ]


####################################
# Figure 2. 
# Treatment effects of party cues on policy preferences with 95% CIs (high-salience issue)
####################################

party2cue.lm <- lm(rcs_pdr~expcond_2ndvign*pid, data=data.second)

party2cue.rg1 <- ref.grid(party2cue.lm)
party2cue.lsm <- lsmeans(party2cue.lm, c("expcond_2ndvign", "pid"))
party2cue.sum <- summary(party2cue.lsm)

clabel <- as.data.frame(paste(as.character(party2cue.sum$pid), as.character(party2cue.sum$expcond_2ndvign)))
party2cue.sum2 <- cbind(party2cue.sum, clabel)
names(party2cue.sum2)[8]<-paste("label")

party2cue.sum2$label <- factor(party2cue.sum2$label, as.character(party2cue.sum2$label))

theme.novpadding <- 
  list(layout.heights = 
         list(top.padding = 3, 
              main.key.padding = 0, 
              key.axis.padding = 0, 
              axis.xlab.padding = 0, 
              xlab.key.padding = 0, 
              key.sub.padding = 0, 
              bottom.padding = 3), 
       layout.widths = 
         list(left.padding = 0, 
              key.ylab.padding = 0, 
              ylab.axis.padding = 0, 
              axis.key.padding = 0, 
              right.padding = 0),
       axis.text=list(cex=1.3),
       par.sub.text=list(cex=1, just="left",x = grid::unit(5, "mm")))

xyplot(expcond_2ndvign~lsmean | pid, data=party2cue.sum2,
       scales=list(alternating=FALSE,
                   y=list(relation="same")), 
       main=list("Right to collective defence", cex=1.3),
       xlab=list("Disapproval rate (%)\n", cex=1.3), ylab=NULL, 
       low95=party2cue.sum2$lower.CL, up95=party2cue.sum2$upper.CL, 
       panel = function(x,y,subscripts, low95, up95,...){
         panel.segments(low95[subscripts], y, 
                        up95[subscripts], y, col="#000000")
         panel.points(x,y, pch=16, col="#000000")
       },
       xlim=c(min(party2cue.sum2$lower.CL)-10, max(party2cue.sum2$upper.CL)+15), 
       par.strip.text=list(cex=1.3), 
       par.settings=theme.novpadding, 
       layout=c(3,1), as.table=T, between=list(x=0.5))


####################################
# Figure 4. 
# Treatment effects of press cues on policy preferences with 95% CIs (high-salience issue)
####################################

press2cue.lm <- lm(rcs_pdr ~ expcond_2ndvign*newssub, data=data.first)

press2cue.rg1 <- ref.grid(press2cue.lm)
press2cue.lsm <- lsmeans(press2cue.lm, c("expcond_2ndvign", "newssub"))
press2cue.sum <- summary(press2cue.lsm)

clabel <- as.data.frame(paste(as.character(press2cue.sum$newssub), as.character(press2cue.sum$expcond_2ndvign)))
press2cue.sum2 <- cbind(press2cue.sum, clabel)
names(press2cue.sum2)[8]<-paste("label")

press2cue.sum2$label <- factor(press2cue.sum2$label, as.character(press2cue.sum2$label))

xyplot(expcond_2ndvign~lsmean | newssub, data=press2cue.sum2,
       scales=list(alternating=FALSE,
                   y=list(relation="same")), 
       main=list("Right to collective defence", cex=1.3),
       xlab=list("Disapproval rate (%)\n", cex=1.3), ylab=NULL, 
       low95=press2cue.sum2$lower.CL, up95=press2cue.sum2$upper.CL, 
       panel = function(x,y,subscripts, low95, up95,...){
         panel.segments(low95[subscripts], y, 
                        up95[subscripts], y, col="#000000")
         panel.points(x,y, pch=16, col="#000000")
       },
       xlim=c(min(press2cue.sum2$lower.CL)-10, max(press2cue.sum2$upper.CL)+20), 
       par.strip.text=list(cex=1.3), 
       par.settings=theme.novpadding, 
       layout=c(4,1), as.table=T, between=list(x=0.5))

